<?php 
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class Category extends Error
{
	public function index(Request $request)
	{	
		// 遍历全部分类
		$list = $this->cateList();

		// 遍历全部书籍（排序）
		$res = $request->param(); 
		empty($res['where'])? $res['where'] = 0 : $res['where'] = $res['where'];
		switch ($res['where']) {
			case '1': $res['where'] = 'ctime';break;
			case '2': $res['where'] = 'sale desc';break;
			case '3': $res['where'] = 'price';break;
			default:  $res['where'] = 'hot desc';break;
		}
		$data = $this->showAll($res['where']);

		// 分页
        $page = $data->render();

		 return view('category/index',[
            'title'=>'图书推荐',
            'data'=>$list,
            'list'=>$data,
            'page'=>$page
        ]);
	}


	//	加载二级分类页 
	public function showSon(Request $request)
	{	
		$res = $request->param();

		// 排序
		$where = $res['where'];
		switch ($res['where']) {
			case '1': $res['where'] = 'ctime';break;
			case '2': $res['where'] = 'sale desc';break;
			case '3': $res['where'] = 'price desc';break;
			default:  $res['where'] = 'hot desc';break;
		}

		if ( !empty($res['pid']) ) {
			// 处理数据
			$res['id']= ''; $two['name'] = '';
			$one = db('category')->field(['name'])->where('id','=',$res['pid'])->find();

			// 遍历二级分类池
			$child = db('category')->field(['id'])->where('pid','=',$res['pid'])->select();
			if (empty($child)) {
				$data = db::table('s75_data')->field(['id','name','price','author','face'])->order($where)
					->where('cid','=',$res['pid'])->paginate(20);
				$list = null;
			}else{
				foreach ($child as $k => $v) { $child[$k] = $v['id']; }

				// 查询书本信息
				$data = db::table('s75_data')->field(['id','name','price','author','face'])->order($where)
					->where('cid','in',$child)->paginate(20);
				// 查询二级分类
				$list = db('category')->field(['name','id'])->where('pid','=',$res['pid'])->select();
			}
			
		}else{
			// 处理数据
			$two = db('category')->field(['name','pid'])->find($res['id']);
			$one = db('category')->field(['name','id'])->where('id','=',$two['pid'])->find();
			$res['pid']= $one['id'];

			// 查询二级分类
			$list = db('category')->field(['name','id'])->where('pid','=',$one['id'])->select();
			
			// 查询书本信息
			$data = db('data')->field(['id','name','price','author','face'])->order($where)
				->where('cid','=',$res['id'])->paginate(20);

		}
		// var_dump($id); die;
		$page = $data->render();

		return view('category/showSon',[
			'title'=>$one['name'].'_'.$two['name'],
			'one'=>$one['name'],
			'two'=>$two['name'],
			'list'=>$list,
			'data'=>$data,
			'id'=>$res['id'],
			'pid'=>$res['pid'],
			'page'=>$page
		]);
	}

	// 遍历全部分类
	public function cateList()
	{
		$list = db('category')->field(['id','name'])->where('pid','=','0')->select();
		$res = db('category')->field(['id','pid','name'])->where('pid','>','0')->select();

		foreach ($list as $k => $v) {
			$list[$k]['list'] = [];
			foreach ($res as $ko => $vo) {
				if ($v['id']==$vo['pid']) {
					$vo = array_diff_key($vo,['pid'=>'1']);
					$list[$k]['list'][] = $vo;
				}
			}		
		}
		// echo '<pre>';print_r($list); die;
		return $list;
	}

	// 榜单页
	public function rank($id)
	{	
		switch ($id) {
			case '1':// 热销
				$data = $this->showAll('ctime');
				break;
			case '2':// 新书
				$data = $this->showAll('search desc');
				break;
			case '3':// 热搜
				$data = $this->showAll('hot desc');
				break;
			default:// 销量
				$data = $this->showAll('sale desc');
				break;
		}
		
		// 分页
		 $page = $data->render();
		 return view('category/rank',[
            'title'=>'榜单',
            'data'=>$data,
            'page'=>$page
        ]);
	}

	// 遍历全部书籍(排序)
	public function showAll($where)
	{
		 $data = Db::field('d.id, d.name, price, author, face')
        ->table(['s75_data'=>'d','s75_category'=>'c'])
        ->where('c.id = d.cid')
        ->order($where)
        ->paginate(20);
        return $data;
	}
		


}


	






























